/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.balisunrise.common.VisualAction;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.common.abstracts.AbstractVisualEntity;
import org.balisunrise.swing.transaction.panel.ActionPanel;
import org.balisunrise.swing.transaction.events.VisualActionEvent;

/**
 *
 * @author Glauco
 */
public class EntityActions extends AbstractVisualEntity
        implements org.balisunrise.common.EntityActions
        <VisualAction<JButton>, JPanel>{

    private VisualAction<JButton> confirmationAction;
    private VisualAction<JButton> cancellationAction;
    private List<VisualAction<JButton>> actions;
    private ActionPanel visualRepresentation;

    public EntityActions() {
        actions = new ArrayList<VisualAction<JButton>>();
    }

    public List<VisualAction<JButton>> getActions() {
        return actions;
    }

    public void setActions(List<VisualAction<JButton>> actions) {

        if(actions == null)
            return ;

        this.actions = actions;
    }

    public void setVisualRepresentation(ActionPanel visualRepresentation) {
        this.visualRepresentation = visualRepresentation;
    }

    public void setConfirmationAction(VisualAction<JButton> action) {
        this.confirmationAction = action;
        visualRepresentation.setConfirmationButton(
                action.getVisualRepresentation());
    }

    public void setCancellationAction(VisualAction<JButton> action) {
        this.cancellationAction = action;
        visualRepresentation.setCancellationButton(
                action.getVisualRepresentation());
    }

    public void confirm() {
        if(confirmationAction != null)
            confirmationAction.perform(
                    new VisualActionEvent(confirmationAction));
    }

    public void cancel() {
        if (cancellationAction != null)
            cancellationAction.perform(
                    new VisualActionEvent(cancellationAction));
    }

    public void addAction(VisualAction<JButton> action) {

        if(action == null)
            return ;

        getActions().add(action);

        visualRepresentation.addAction(action.getVisualRepresentation()
                , getActions().size());
    }

    public void addAction(VisualAction<JButton> action, int index) {

        if(action == null)
            return ;

        if(getActions().size() < index)
            getActions().add(action);

        if(index < 0)
            getActions().add(0, action);

        getActions().add(index, action);

        visualRepresentation.addAction(action.getVisualRepresentation()
                , index);
    }

    public void doAction(int index) {
        VisualAction action = getActions().get(index);
        if (action == null){
            action.perform(
                    new VisualActionEvent(action));
        }
    }

    public JPanel getVisualRepresentation() {
        return visualRepresentation;
    }

    /**
     * Retorna o tipo do VisualEntity.<BR>
     * Sempre retornará ENTITY_ACTIONS.
     * @return VisualEntityType.ENTITY_ACTIONS
     */
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.ENTITY_ACTIONS;
    }
}
